package com.dhcc.bpm.modules.safe.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhcc.bpm.common.annotation.Log;
import com.dhcc.bpm.common.enums.BusinessType;
import com.dhcc.bpm.common.enums.ConfigType;
import com.dhcc.bpm.common.redisMessage.RedisMessageSender;
import com.dhcc.bpm.modules.api.service.IApiService;
import com.dhcc.bpm.modules.api.vo.ApiVO;
import com.dhcc.bpm.modules.orga.service.IAppService;
import com.dhcc.bpm.modules.orga.service.IOrgaService;
import com.dhcc.bpm.modules.orga.vo.AppVO;
import com.dhcc.bpm.modules.orga.vo.OrgaVO;
import com.dhcc.bpm.modules.safe.entity.AccessControl;
import com.dhcc.bpm.modules.safe.service.*;
import com.dhcc.bpm.modules.safe.vo.AccessControlVO;
import com.dhcc.bpm.modules.safe.vo.IpControlVO;
import com.dhcc.bpm.modules.system.service.ISerialNumberService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.Map;
/**
 * @ClassName AccessControlController
 * @Description  API访问控制表 控制器
 * @Author daina
 * @Date 2020/2/21 14:16
 * @Version V1.0
 * @Copyright: 2020 东华软件 . All rights reserved.
 */
@RestController
@AllArgsConstructor
@RequestMapping("bpm-safe/accesscontrol")
@Api(value = "API访问控制表", tags = "API访问控制表接口")
public class AccessControlController extends BladeController {
	private static final Logger log = LoggerFactory.getLogger(AccessControlController.class);

	private IAccessControlService accessControlService;

	private ISerialNumberService serialNumberService;
	@Autowired
	private RedisMessageSender redisMessageSender;
	@Autowired
	private IApiService apiService;
	@Autowired
	private IOrgaService orgaService;
	@Autowired
	private IAppService appService;
	@Autowired
	private IIpControlService ipControlService;
	@Autowired
	private IApiControlService apiControlService;
	@Autowired
	private IOrgaControlService orgaControlService;
	@Autowired
	private IAppControlService appControlService;
	/**
	 * @Description 详情
	 * @Author daina
	 * @Date 21:24 2020/2/21
	 * @param accessControl
	 * @return org.springblade.core.tool.api.R<com.dhcc.bpm.modules.safe.entity.AccessControl>
	 **/
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入accessControl")
	@ApiLog("详情")
	@Log(title = "详情",businessType = BusinessType.SELECT)
	public R<AccessControl> detail(AccessControl accessControl) {
		AccessControl detail = accessControlService.getOne(Condition.getQueryWrapper(accessControl));
		return R.data(detail);
	}

	/**
	 * @Description 分页 API访问控制表
	 * @Author daina
	 * @Date 21:22 2020/2/21
	 * @param accessControl
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.safe.entity.AccessControl>>
	 **/
	@GetMapping("/list")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "controlScope", value = "控制范围", paramType = "query", dataType = "string"),
			@ApiImplicitParam(name = "controlName", value = "控制名称", paramType = "query", dataType = "string"),
			@ApiImplicitParam(name = "controlCode", value = "控制编号", paramType = "query", dataType = "string"),
			@ApiImplicitParam(name = "strategyType", value = "策略类型", paramType = "query", dataType = "string"),
			@ApiImplicitParam(name = "controlStatus", value = "控制状态", paramType = "query", dataType = "string")
	})
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入accessControl")
	@ApiLog("查询API访问控制表")
	@Log(title = "查询API访问控制表",businessType = BusinessType.SELECT)
	public R<IPage<AccessControl>> list(@ApiIgnore @RequestParam Map<String, Object> accessControl, Query query) {
		IPage<AccessControl> pages = accessControlService.page(Condition.getPage(query), Condition.getQueryWrapper(accessControl,AccessControl.class));
		return R.data(pages);
	}

	/**
	 * @Description 自定义分页 API访问控制表
	 * @Author daina
	 * @Date 21:23 2020/2/21
	 * @param accessControl
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.safe.vo.AccessControlVO>>
	 **/
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入accessControl")
	@ApiLog("查询API访问控制表")
	@Log(title = "查询API访问控制表",businessType = BusinessType.SELECT)
	public R<IPage<AccessControlVO>> page(AccessControlVO accessControl, Query query) {
		IPage<AccessControlVO> pages = accessControlService.selectAccessControlPage(Condition.getPage(query), accessControl);
		return R.data(pages);
	}

	/**
	 * @Description 新增 API访问控制表
	 * @Author daina
	 * @Date 21:24 2020/2/21
	 * @param accessControl
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入accessControl")
	@ApiLog("新增")
	@Log(title = "新增",businessType = BusinessType.INSERT)
	public R save(@Valid @RequestBody AccessControl accessControl) {
		return R.status(accessControlService.save(accessControl));
	}

	/**
	 * @Description 修改 API访问控制表
	 * @Author daina
	 * @Date 21:25 2020/2/21
	 * @param accessControl
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入accessControl")
	@ApiLog("修改")
	@Log(title = "修改",businessType = BusinessType.UPDATE)
	public R update(@Valid @RequestBody AccessControl accessControl) {
		return R.status(accessControlService.updateById(accessControl));
	}

	/**
	 * @Description 新增或修改 API访问控制表
	 * @Author daina
	 * @Date 21:25 2020/2/21
	 * @param accessControl
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入accessControl")
	@ApiLog("新增Api访问控制")
	@Log(title = "新增Api访问控制", businessType = BusinessType.INSERT)
	public R submit(@Valid @RequestBody AccessControl accessControl) {
		if (accessControl.getId() == null){
			// 设置控制编号
			String code = serialNumberService.generateSerialNumberByModelCode("opc_access_control");
			accessControl.setControlCode(code);
		}
		accessControl.setControlName(accessControl.getControlName().trim());
		accessControl.setRefreshFlag("1");
		try {
			return R.status(accessControlService.saveOrUpdate(accessControl));
		} catch (Exception e) {
			if (e.getMessage().contains("for key 'control_name_idx'") || e.getMessage().contains("ORA-00001")) {
				return R.fail(500, "该控制名称已存在");
			}
			return R.fail(500, "数据库异常");
		} finally {

		}
	}


	/**
	 * @Description 删除 API访问控制表
	 * @Author daina
	 * @Date 21:25 2020/2/21
	 * @param ids
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	@ApiLog("删除API访问控制")
	@Log(title = "删除API访问控制", businessType = BusinessType.DELETE)
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(accessControlService.deleteLogic(Func.toLongList(ids)));
	}

	/**
	 * @Description 刷新API黑白名单
	 * @Author daina
	 * @Date 21:25 2020/2/21
	 * @param env
	 * @param controlScope
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/refreshApi")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "刷新API黑白名单")
	@ApiLog("刷新API黑白名单")
	@Log(title = "刷新API黑白名单", businessType = BusinessType.REFRESH)
	public R refreshAppSecret(@ApiParam(value = "主键集合", required = true) @RequestParam String env,@RequestParam String controlScope) {
		try {
			String message = redisMessageSender.format(ConfigType.APIWHITEBLACK.name(), "刷新API黑白名单");
			redisMessageSender.sendMessageToGateway("", message);
			AccessControl accessControl = new AccessControl();

			// RefreshFlag 2：已刷新
			accessControl.setRefreshFlag("2");
			accessControl.setEnv(env);
			accessControl.setControlScope(controlScope);
			int result = accessControlService.updateRefreshFlag(accessControl);
			if(result>=0){
				return R.status(true);
			}
			return R.status(false);
		}catch (Exception e){
			log.error("刷新API黑白名单失败:{}", e);
			return R.status(false);
		}
	}

	/**
	 * @Description 自定义分页 api表
	 * @Author daina
	 * @Date 21:26 2020/2/21
	 * @param api
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.api.vo.ApiVO>>
	 **/
	@GetMapping("/selectControllerApiList")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入api")
	@ApiLog("查询")
	@Log(title = "查询",businessType = BusinessType.SELECT)
	public R<IPage<ApiVO>> selectControllerApiPage(@ApiIgnore ApiVO api , Query query) {
		IPage<ApiVO> pages = apiService.selectControllerPage(Condition.getPage(query), api);
		return R.data(pages);
	}

	/**
	 * @Description 自定义分页 机构信息表
	 * @Author daina
	 * @Date 21:26 2020/2/21
	 * @param orga
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.orga.vo.OrgaVO>>
	 **/
	@GetMapping("/selectControllerOrgaList")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入orga")
	@ApiLog("查询机构信息")
	@Log(title = "查询机构信息",businessType = BusinessType.SELECT)
	public R<IPage<OrgaVO>> selectControllerOrgaPage(@ApiIgnore OrgaVO orga, Query query) {
		IPage<OrgaVO> pages = orgaService.selectControllerOrga(Condition.getPage(query), orga);
		return R.data(pages);
	}

	/**
	 * @Description 自定义分页 应用/渠道信息
	 * @Author daina
	 * @Date 21:26 2020/2/21
	 * @param app
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.orga.vo.AppVO>>
	 **/
	@GetMapping("/selectControllerAppList")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入app")
	@ApiLog("查询应用信息")
	@Log(title = "查询应用信息",businessType = BusinessType.SELECT)
	public R<IPage<AppVO>> selectControllerOrgaListPage(@ApiIgnore AppVO app, Query query) {
		IPage<AppVO> pages = appService.selectControllerAppListPage(Condition.getPage(query), app);
		return R.data(pages);
	}

	/**
	 * @Description 自定义分页 API访问控制IP关联表
	 * @Author daina
	 * @Date 21:26 2020/2/21
	 * @param ipControl
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.safe.vo.IpControlVO>>
	 **/
	@GetMapping("/selectControllerIpList")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入ipControl")
	@ApiLog("查询")
	@Log(title = "查询",businessType = BusinessType.SELECT)
	public R<IPage<IpControlVO>> selectControllerIpListpage(@ApiIgnore IpControlVO ipControl, Query query) {
		IPage<IpControlVO> pages = ipControlService.selectIpControlPage(Condition.getPage(query), ipControl);
		return R.data(pages);
	}

	/**
	 * @Description 查询未分配的api列表
	 * @Author daina
	 * @Date 21:27 2020/2/21
	 * @param api
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.api.vo.ApiVO>>
	 **/
	@GetMapping("/selectUnControllerApiList")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入api")
	@ApiLog("查询未分配的接口列表")
	@Log(title = "查询未分配的接口列表",businessType = BusinessType.SELECT)
	public R<IPage<ApiVO>> selectUnControllerApiList(@ApiIgnore ApiVO api, Query query){
		IPage<ApiVO> pages = apiService.selectUnControllerApiList(Condition.getPage(query), api);
		return R.data(pages);
	}

	/**
	 * @Description 批量选择api
	 * @Author daina
	 * @Date 21:27 2020/2/21
	 * @param opcControlId
	 * @param ids
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/AuthControllerApiAll")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入api")
	@ApiLog("批量选择api")
	@Log(title = "批量选择api", businessType = BusinessType.INSERT)
	public R AuthControllerApiAll(@ApiParam(name ="opcControlId", value = "控制id") @RequestParam Long  opcControlId,@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return  R.status(apiControlService.batchControllerApi(opcControlId,ids));
	}

	/**
	 * @Description 批量删除API
	 * @Author daina
	 * @Date 21:27 2020/2/21
	 * @param opcControlId
	 * @param ids
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/cancelAuthControllerApiAll")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	@ApiLog("批量删除API")
	@Log(title = "批量删除API", businessType = BusinessType.DELETE)
	public R cancelAuthControllerApiAll(@ApiParam(name ="opcControlId", value = "控制id") @RequestParam Long  opcControlId,@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		int apis = apiControlService.deleteApis(opcControlId, ids);
		if(apis>0){
			return  R.status(true);
		}else{
			return  R.status(false);
		}
	}

	/**
	 * @Description 查询未分配的机构列表
	 * @Author daina
	 * @Date 21:27 2020/2/21
	 * @param orga
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.orga.vo.OrgaVO>>
	 **/
	@GetMapping("/selectUnControllerOrgaList")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "orga")
	@ApiLog("查询未分配的机构信息")
	@Log(title = "查询未分配的机构信息",businessType = BusinessType.SELECT)
	public R<IPage<OrgaVO>> selectUnControllerOrgaList(@ApiIgnore OrgaVO orga, Query query){
		IPage<OrgaVO> pages = orgaService.selectUnControllerOrgaList(Condition.getPage(query), orga);
		return R.data(pages);
	}

	/**
	 * @Description 批量选择机构
	 * @Author daina
	 * @Date 21:27 2020/2/21
	 * @param opcControlId
	 * @param ids
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/AuthControllerOrgaAll")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入orga")
	@ApiLog("批量选择机构")
	@Log(title = "批量选择机构", businessType = BusinessType.INSERT)
	public R AuthControllerOrgaAll(@ApiParam(name ="opcControlId", value = "控制id") @RequestParam Long  opcControlId,@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return  R.status(orgaControlService.batchControllerOrga(opcControlId,ids));
	}
	/**
	 * @Description 批量取消机构信息
	 * @Author daina
	 * @Date 21:27 2020/2/21
	 * @param opcControlId
	 * @param ids
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/cancelAuthControlleOrgaAll")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	@ApiLog("批量取消机构")
	@Log(title = "批量取消机构", businessType = BusinessType.DELETE)
	public R cancelAuthControllerMerInfoAll(@ApiParam(name ="opcControlId", value = "控制id") @RequestParam Long  opcControlId,@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		int apis = orgaControlService.deleteOrgas(opcControlId, ids);
		if(apis>0){
			return  R.status(true);
		}else{
			return  R.status(false);
		}
	}

	/**
	 * @Description 查询未分配的渠道列表
	 * @Author daina
	 * @Date 21:28 2020/2/21
	 * @param app
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.orga.vo.AppVO>>
	 **/
	@GetMapping("/selectUnControllerAppList")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入app")
	@ApiLog("查询未分配的渠道列表信息")
	@Log(title = "查询未分配的渠道列表信息",businessType = BusinessType.SELECT)
	public R<IPage<AppVO>> selectUnControllerAppList(AppVO app, Query query){
		IPage<AppVO> pages = appService.selectUnControllerAppList(Condition.getPage(query), app);
		return R.data(pages);
	}

	/**
	 * @Description 批量选择渠道
	 * @Author daina
	 * @Date 21:28 2020/2/21
	 * @param opcControlId
	 * @param ids
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/AuthControllerAppAll")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入app")
	@ApiLog("批量选择渠道")
	@Log(title = "批量选择渠道", businessType = BusinessType.INSERT)
	public R AuthControllerAppAll(@ApiParam(name ="opcControlId", value = "控制id") @RequestParam Long  opcControlId,@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return  R.status(appControlService.batchControllerApp(opcControlId,ids));
	}

	/**
	 * @Description 批量取消渠道信息
	 * @Author daina
	 * @Date 21:28 2020/2/21
	 * @param opcControlId
	 * @param ids
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/cancelAuthControllerAppMsgAll")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	@ApiLog("批量取消渠道信息")
	@Log(title = "批量取消渠道信息", businessType = BusinessType.DELETE)
	public R cancelAuthControllerAppMsgAll(@ApiParam(name ="opcControlId", value = "控制id") @RequestParam Long  opcControlId,@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		int apis = appControlService.deleteApps(opcControlId, ids);
		if(apis>0){
			return  R.status(true);
		}else{
			return  R.status(false);
		}
	}

	/**
	 * @Description 修改 控制状态
	 * @Author daina
	 * @Date 21:28 2020/2/21
	 * @param accessControl
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/changeStatus")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入AccessControl")
	@ApiLog("修改控制状态")
	@Log(title = "修改控制状态", businessType = BusinessType.UPDATE)
	public R changeStatus(@Valid @RequestBody AccessControl accessControl) {

		// RefreshFlag 1：未刷新
		accessControl.setRefreshFlag("1");
		return R.status(accessControlService.updateById(accessControl));
	}

}
